var datePicker = require('../../utils/dateSetting.js')
//设定当前的时间，将其设定为常量
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startdate: "",
    stopdate: "",
    aggregatedate: '请选择时间',
    multiArray: [],
    multiIndex: [0, 0, 0, 0, 0],
    choose_year: "",
    link: true,
    group_id:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id;
    this.setData({
      multiArray: [
        [year + "年", year + 1 + "年", year + 2 + "年",year + 3 + "年",year + 4 + "年"],
        // datePicker.determineYear(),
        datePicker.determineMonth(),
        datePicker.determineDay(year,month),
        datePicker.determineHour(),
        datePicker.determineMinute()
      ],
      group_id:id
    })
    wx.request({
      url: 'http://www.house.com/groupDetails',
      method: "POST",
      data: {
        id: id
      },
      success: (res) => {
        let detail = res.data.data;
        this.setData({
          detail: detail
        })
        let startdate = detail.start_date;
        let stopdate = detail.stop_date;
        this.setData({
          startdate: startdate,
          stopdate: stopdate
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //最后呈现时间的函数。
  bindMultiPickerChange: function (e) {
    var dateStr = this.data.multiArray[0][this.data.multiIndex[0]] +
      this.data.multiArray[1][this.data.multiIndex[1]] +
      this.data.multiArray[2][this.data.multiIndex[2]] +
      this.data.multiArray[3][this.data.multiIndex[3]] +
      this.data.multiArray[4][this.data.multiIndex[4]];
    this.setData({
      aggregatedate: dateStr,
      link: false
    })
 
  },
  //当时间选择器呈现并进行滚动选择时间时调用该函数。
  bindMultiPickerColumnChange: function (e) {
    switch (e.detail.column) {
      case 0:
        //记录改变的年的值
        let year = this.data.multiArray[0][e.detail.value];
        this.setData({
          choose_year: year.substring(0, year.length - 1)
        })
        break;
      case 1:
        //根据选择的年与月，确定天数，并改变multiArray中天的具体值
        let month = this.data.multiArray[1][e.detail.value];
        let dayDates = datePicker.determineDay(this.data.choose_year, month.substring(0, month.length - 1));
        //这里需要额外注意，改变page中设定的data，且只要改变data中某一个值，可以采用下面这种方法
        this.setData({
          ['multiArray[2]']: dayDates
        })
        break;
    }
    //同上，上面改变的是二维数组中的某一个一维数组，这个是改变一个一维数组中某一个值
    this.setData({
      ["multiIndex[" + e.detail.column + "]"]: e.detail.value
    })
  },
  start(e){
    let group_id=this.data.group_id;
    let aggregate_address=e.detail.value.aggregate_address;
    let license_plate_number=e.detail.value.license_plate_number;
    let  aggregate_datetime=this.data.aggregatedate;
    if(aggregate_address==""){
      wx.showModal({
       title:"提醒",
       content:"集合地址为空，请重新填写"
      })
      return;
    }
    if(license_plate_number==""){
      wx.showModal({
       title:"提醒",
       content:"车牌号为空，请重新填写"
      })
      return;
    }
    if(aggregate_datetime=="请选择时间"){
      wx.showModal({
       title:"提醒",
       content:"集合时间为空，请重新填写"
      })
      return;
    }
    wx.request({
      url: 'http://www.house.com/startGroup',
      method:"POST",
      data:{
        group_id:group_id,
        aggregate_address:aggregate_address,
        license_plate_number:license_plate_number,
        aggregate_datetime:aggregate_datetime
      },
      success:(res)=>{
        let code=res.data.code;
        if(code==200){
          wx.showModal({
           title:"提示",
          content:"开团成功"
          })
        }
        if(code==2002){
          wx.showModal({
            title:"提醒",
           content:"该团已开启"
           })
        }
        if(code==2003){
          wx.showModal({
            title:"提醒",
           content:"该团未在客户端显示,请先开启"
           })
        }
      }
    })
  }
})